import { Inject, Provide } from '@midwayjs/decorator'
import { Context } from 'egg'

import { CODE, getMessage } from '../constants/code'

@Provide()
export class BaseController {

  @Inject()
  ctx: Context

  ok(data?: any) {
    const res = {
      code: CODE['SUCCESS'],
      message: this.ctx.__('SUCCESS')
    }

    if (data) {
      res['data'] = data
    }

    return res
  }

  notOk(code, message?: string) {
    if (code === undefined) {
      return {
        code: CODE['FAIL'],
        message: this.ctx.__('FAIL')
      }
    }

    return {
      code,
      message: message || this.ctx.__(getMessage(code))
    }
  }
}
